#include "Vector3D.h"

Vector3D Vector3D::Add(const Vector3D &left, const Vector3D &right)
{
	Vector3D result;
	result.x = left.x + right.x;
	result.y = left.y + right.y;
	result.z = left.z + right.z;
	return result;
}

Vector3D Vector3D::Substract(const Vector3D &left, const Vector3D &right)
{
	Vector3D result;
	result.x = left.x - right.x;
	result.y = left.y - right.y;
	result.z = left.z - right.z;
	return result;
}

Vector3D Vector3D::Scale(const Vector3D &vect, const float scale)
{
	Vector3D result;
	result.x = vect.x * scale;
	result.y = vect.y * scale;
	result.z = vect.z * scale;

	return result;
}

float Vector3D::Dot(const Vector3D &left, const Vector3D &right)
{
	return (left.x*right.x + left.y*right.y + left.z*right.z);
}

Vector3D Vector3D::CrossLH(const Vector3D &left, const Vector3D &right)
{
	Vector3D result;
	result.x = left.y*right.z - left.z*right.y;
	result.y = left.z*right.x - left.x*right.z;
	result.z = left.x*right.y - left.y*right.x;

	return result;
}
Vector3D Vector3D::CrossRH(const Vector3D &left, const Vector3D &right)
{
	return CrossLH(right, left);
}

void Vector3D::MoveBy(const Vector3D &by, Vector3D &tomove)
{
	tomove = Add(tomove, by);
}

float Vector3D::Length(const Vector3D &vect)
{
	return sqrtf(LengthSq(vect));
}

float Vector3D::LengthSq(const Vector3D &vect)
{
	return vect.x*vect.x + vect.y*vect.y + vect.z*vect.z;
}

bool Vector3D::Normalize(Vector3D &vect)
{
	float length = Length(vect);

	if (length != 0) 
	{
		vect.x /= length;
		vect.y /= length;
		vect.z /= length;

		return true;
	} 

	return false;
}

void Vector3D::ScaleBy(Vector3D &vect, const float scale)
{
	vect = Scale(vect, scale);
}

bool Vector3D::Equals(const Vector3D &left, const Vector3D &right)
{
	//return (left.x == right.x  &&  left.y == right.y  &&  left.z == right.z);
	//if (fabs(result - expectedResult) < 0.00001)
	return (fabs(left.x - right.x) < EPSILON  &&  
		    fabs(left.y - right.y) < EPSILON  &&
			fabs(left.z - right.z) < EPSILON  );
}